const Koa = require('koa')
const R = require('ramda')
const MIDDLEWARES = ['router']
const { resolve } = require('path')
const {initSchemas, connect} = require('./db/index')

const useMiddlewares = (app) => {
    R.map(
        R.compose(
            R.forEachObjIndexed(
                initFunc => initFunc(app)
            ),
            require,        //相当于import { xx} from '...'
            name => resolve(__dirname, `./middlewares/${name}`)
        )
    )(MIDDLEWARES)
}

;(async () => {
    process.on('unhandledRejection', error => {
        console.error('unhandledRejection', error);
        process.exit(1) // To exit with a 'failure' code
    });

    await initSchemas()

    connect()

    const app = new Koa()

    useMiddlewares(app)

    app.listen(2333)
    
    console.log('now is listening on 2333')
})()